{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "#load \"..\\Extension\\LoadBaseTool.csx\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [],
   "source": [
    "public class Person\n",
    "{\n",
    "    public string Name { get; set; }\n",
    "    public int Age { get; set; }\n",
    "    public string Address1 { get; set; }\n",
    "    public string Address2 { get; set; }\n",
    "    public string Address3 { get; set; }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name,Age,Address1,Address2,Address3\r\n"
     ]
    }
   ],
   "source": [
    "using System.Reflection;\n",
    "\n",
    "Person person = new Person\n",
    "{\n",
    "    Name = \"Bill\",\n",
    "    Age = 20,\n",
    "    Address1 = \"Address1\",\n",
    "    Address2 = \"Address2\",\n",
    "    Address3 = \"Address3\"\n",
    "};\n",
    "\n",
    "Type type = person.GetType();\n",
    "PropertyInfo[] properties = type.GetProperties();\n",
    "\n",
    "string _header = string.Join(\",\", properties.Select(p => p.Name));\n",
    "_header.Dump();"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Bill,20,AAA21,BBB22,CCC23\n",
      "Tom,30,AAA31,BBB32,CCC33\n",
      "John,40,AAA41,BBB42,CCC43\n",
      "Jerry,50,AAA51,BBB52,CCC53\n"
     ]
    }
   ],
   "source": [
    "// 生成一个Person的列表，并且每个Person的属性值都是随机的\n",
    "List<Person> persons = \n",
    "[\n",
    "    new Person { Name = \"Bill\", Age = 20, Address1 = \"AAA21\", Address2 = \"BBB22\", Address3 = \"CCC23\" },\n",
    "    new Person { Name = \"Tom\", Age = 30, Address1 = \"AAA31\", Address2 = \"BBB32\", Address3 = \"CCC33\" },\n",
    "    new Person { Name = \"John\", Age = 40, Address1 = \"AAA41\", Address2 = \"BBB42\", Address3 = \"CCC43\" },\n",
    "    new Person { Name = \"Jerry\", Age = 50, Address1 = \"AAA51\", Address2 = \"BBB52\", Address3 = \"CCC53\" }\n",
    "];\n",
    "\n",
    "foreach (var person in persons)\n",
    "{\n",
    "    string _line = string.Join(\",\", properties.Select(p => p.GetValue(person)));\n",
    "    _line.Dump();\n",
    "}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name, Address1, Address3\n",
      "Bill,AAA21,CCC23\n",
      "Tom,AAA31,CCC33\n",
      "John,AAA41,CCC43\n",
      "Jerry,AAA51,CCC53\n"
     ]
    }
   ],
   "source": [
    "// 通过PropertyName创建PropertyInfo\n",
    "string propertyName = \"Name,Address1,Address3\";\n",
    "string[] propertyNames = propertyName.Split(',');\n",
    "propertyNames.Dump();\n",
    "string[] propertyValues = persons.Select(p => string.Join(\",\", propertyNames.Select(n => type.GetProperty(n).GetValue(p)))).ToArray();\n",
    "foreach (var value in propertyValues)\n",
    "{\n",
    "    value.Dump();\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Name: Bill\n",
      "Age: 25\n",
      "Address1: AAA\n",
      "Address2: BBB\n",
      "Address3: CCC\n"
     ]
    }
   ],
   "source": [
    "Person person = new Person { Name = \"Bill\", Age = 25, Address1 = \"AAA\", Address2 = \"BBB\", Address3 = \"CCC\" };\n",
    "\n",
    "Dictionary<string, object> personDictionary = person.GetType().GetProperties()\n",
    "    .ToDictionary(prop => prop.Name, prop => prop.GetValue(person));\n",
    "\n",
    "foreach (var kvp in personDictionary)\n",
    "{\n",
    "    Console.WriteLine($\"{kvp.Key}: {kvp.Value}\");\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "List<int> list1 = new List<int> { 1, 2, 3, 4, 5 };\n",
    "List<int> list2 = new List<int> { 7, 6, 5, 4, 3, 2, 1 };\n",
    "\n",
    "var result = list2.Except(list1);\n",
    "\n",
    "foreach (var item in result)\n",
    "{\n",
    "    Console.WriteLine(item);\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "dotnet_interactive": {
     "language": "csharp"
    },
    "polyglot_notebook": {
     "kernelName": "csharp"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1, Alice\n",
      "2, Bob\n"
     ]
    }
   ],
   "source": [
    "using System.Data;\n",
    "\n",
    "DataTable dataTable = new DataTable(\"MyTable\");\n",
    "\n",
    "dataTable.Columns.Add(\"ID\", typeof(int));\n",
    "dataTable.Columns.Add(\"Name\", typeof(string));\n",
    "\n",
    "DataRow row1 = dataTable.NewRow();\n",
    "row1[\"ID\"] = 1;\n",
    "row1[\"Name\"] = \"Alice\";\n",
    "dataTable.Rows.Add(row1);\n",
    "\n",
    "DataRow row2 = dataTable.NewRow();\n",
    "row2[\"ID\"] = 2;\n",
    "row2[\"Name\"] = \"Bob\";\n",
    "dataTable.Rows.Add(row2);\n",
    "\n",
    "\n",
    "foreach (DataRow row in dataTable.Rows)\n",
    "{\n",
    "   row.ItemArray.Dump();\n",
    "}"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".NET (C#)",
   "language": "C#",
   "name": ".net-csharp"
  },
  "polyglot_notebook": {
   "kernelInfo": {
    "defaultKernelName": "csharp",
    "items": [
     {
      "aliases": [],
      "languageName": "csharp",
      "name": "csharp"
     }
    ]
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
